import 'dart:io';
import 'package:hotfil_pub_web/shared/loading.dart';
import 'package:image_cropping/image_cropping.dart';
import 'package:image_picker/image_picker.dart';

Future chooseImage(context, {onImageDoneListener}) async {
  try {
    final pickedFile = await ImagePicker()
        .pickImage(source: ImageSource.gallery, maxWidth: 900, maxHeight: 900);
    if (pickedFile == null) {
      return false;
    }
    var mimeType = pickedFile.mimeType;
    print(mimeType);

    var imageBytes = await pickedFile.readAsBytes();

    if (imageBytes != null) {
      ImageCropping.cropImage(
          context: context,
          imageBytes: imageBytes,
          selectedImageRatio: ImageRatio.RATIO_1_1,
          visibleOtherAspectRatios: false,
          onImageDoneListener: onImageDoneListener);
    } else {
      return false;
    }
  } catch (e) {
    showError(message: '上传图片错误,可能图片文件不支持');
  }
}
